package com.wuming.house.service.impl;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.WumingErrorCode;
import com.wuming.house.dao.GroupHouseMapper;
import com.wuming.house.model.GroupHouse;
import com.wuming.house.model.House;
import com.wuming.house.service.GroupHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("groupHouseService")
public class GroupHouseServiceImpl extends BaseServiceImpl implements GroupHouseService {

    @Autowired
    private GroupHouseMapper groupHouseMapper;

    @Override
    public void saveGroupHouse(GroupHouse gh) throws BusinessException {
        groupHouseMapper.insert(gh);
    }

    @Override
    public List<GroupHouse> getHouseByGroupId(Integer groupId, int pageIndex, int pageSize) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("groupId", groupId);
        try {
            List<GroupHouse> groupHouses = (List<GroupHouse>) this.getPageList(GroupHouseMapper.class, "selectHouseByGroup", params, pageIndex, pageSize);
            return groupHouses;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getTotal(Integer groupId) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("groupId", groupId);
        try {
            return this.getTotal(GroupHouseMapper.class, "selectTotal", params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public GroupHouse getHouseGroupByGroupIdAndHouseId(Long groupId, Long houseId) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("groupId", groupId);
        params.put("houseId", houseId);
        try {
            return groupHouseMapper.selectGroupByParams(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<GroupHouse> getAllGroupHousesByParams(Long groupId) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("groupId", groupId);
        try {
            List<GroupHouse> groupHouses = groupHouseMapper.selectAll(params);
            return groupHouses;
        } catch (BusinessException be) {
            logger.error("查询分组房产失败");
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        } catch (Exception e) {
            logger.error("查询分组房产失败");
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public List<GroupHouse> getGroupHousesByPage(Long groupId, int pageIndex, int pageSize) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("groupId", groupId);
        try {
            return (List<GroupHouse>) this.getPageList(GroupHouseMapper.class,"selectGroupSort",params,pageIndex,pageSize);
        } catch (BusinessException be) {
            logger.error("查询分组房产失败");
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        } catch (Exception e) {
            logger.error("查询分组房产失败");
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotalForOnly(Long groupId) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("groupId", groupId);
        try {
            return this.getTotal(GroupHouseMapper.class,"selectTotal",params);
        }catch (Exception e){
            logger.error("查询分组房产总数失败");
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public void deleteHouse(Long groupId,Long houseId) {
        Map<String,Object> params = new HashMap<>();
        params.put("groupId",groupId);
        params.put("houseId",houseId);
        try {
            groupHouseMapper.deleteByPrimaryKey(params);
        }catch (Exception e){
            logger.error("删除分组房产失败");
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public void deleteByHouseId(Long houseId) {
        try {
            groupHouseMapper.deleteByParams(houseId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void modify(GroupHouse groupHouse) {
        try {
            groupHouseMapper.updateByPrimaryKey(groupHouse);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public GroupHouse getByGroupIdAndHouseId(Long groupId, Long houseId) {
        Map<String,Object> params = new HashMap<>();
        params.put("groupId",groupId);
        params.put("houseId",houseId);
        try {
            return groupHouseMapper.selectGroup(params);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
